﻿using HardWareSDK;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using ZK.Common;
using ZK.HardWareSDK;

namespace ZK.HardWareBLL
{
    /// <summary>
    /// 远睿通室内读卡器 api用
    /// </summary>
    public class YRTRSNeaderBLL2
    {
        private YRTReaderSDK reader;
        private InventoryBuffer m_curInventoryBuffer = new InventoryBuffer();
        //盘存操作前，需要先设置工作天线，用于标识当前是否在执行盘存操作
        private bool m_bInventory = false;
        private ReaderSetting m_curSetting = new ReaderSetting();
        //实时盘存锁定操作
        //ISO18000标签连续盘存标识
        private bool m_bContinue = false;
        private int m_nReceiveFlag = 0;
        /// <summary>
        /// 读卡器数据回传函数
        /// </summary>
        /// /// <summary>
        /// 数据回传委托
        /// </summary>
        /// <param name="epc"></param>
        public delegate void GetDataCallbackDelegate(string epc);
        public GetDataCallbackDelegate getDataCallbackEvent;
        System.Timers.Timer timerInventory = new System.Timers.Timer(50);
        public YRTRSNeaderBLL2()
        {
            //初始化访问读写器实例
            reader = new YRTReaderSDK();

            //回调函数
            reader.AnalyCallback = AnalyData;


            timerInventory.Elapsed += timerInventory_Tick;
            timerInventory.Enabled = true;
            timerInventory.AutoReset = false;
            timerInventory.Start();
        }

        private void timerInventory_Tick(object sender, EventArgs e)
        {
            reader.InventoryReal(m_curSetting.btReadId, m_curInventoryBuffer.btRepeat);
            timerInventory.Start();
        }


        private void AnalyData(MessageTran msgTran)
        {

            if (msgTran.AryData.Length != 1)
            {
                int nLength = msgTran.AryData.Length;
                int nEpcLength = nLength - 4;
                string strEPC = CCommondMethod.ByteArrayToString(msgTran.AryData, 3, nEpcLength);
                if (strEPC.Length >= 12)
                {
                    getDataCallbackEvent?.Invoke(strEPC.Substring(0, 12));

                }

            }
        }

        public void BeginS()
        {
            try
            {
                m_curInventoryBuffer.ClearInventoryPar();
                m_curInventoryBuffer.btRepeat = 1;
                m_curInventoryBuffer.bLoopCustomizedSession = false;
                m_curInventoryBuffer.lAntenna.Add(0x00);

                //默认循环发送命令
                if (m_curInventoryBuffer.bLoopInventory)
                {
                    m_bInventory = false;
                    m_curInventoryBuffer.bLoopInventory = false;
                    timerInventory.Enabled = false;
                    return;
                }
                else
                {
                    m_bInventory = true;
                    m_curInventoryBuffer.bLoopInventory = true;
                }
                m_curInventoryBuffer.bLoopInventoryReal = true;
                m_curInventoryBuffer.ClearInventoryRealResult();
                byte btWorkAntenna = m_curInventoryBuffer.lAntenna[m_curInventoryBuffer.nIndexAntenna];
                reader.SetWorkAntenna(m_curSetting.btReadId, btWorkAntenna);
                m_curSetting.btWorkAntenna = btWorkAntenna;
                timerInventory.Enabled = true;
            }
            catch (System.Exception ex)
            {
                Common.Log4.LogManage.WriteErr(ex.ToString());
            }
        }

        public void OpenCom()
        {

            string strException = string.Empty;
            string strComPort = AppConfigHelper.GetAppSettings("ReaderPort");
            int nBaudrate = 115200;

            int nRet = reader.OpenCom(strComPort, nBaudrate, out strException);
            if (nRet != 0)
            {
                Common.Log4.LogManage.WriteErr("连接读写器失败，失败原因： " + strException);
                return;
            }
        }

        public void Close()
        {
            reader.CloseCom();
        }
    }
}
